var NET_TOPO = window.NameSpace || {};

var dp_dataTableconfig = {

	"iDisplayLength" : 10,
	"processing" : true,
	"serverSide" : true,
	"paging" : false,
	"lengthChange" : false,
	"searching" : false,
	"ordering" : false,
	"info" : false,
	"autoWidth" : false,
	"oLanguage" : {
		"sProcessing" : "正在加载中......",
		// "sLengthMenu" : "每页显示 _MENU_ 条记录",
		"sZeroRecords" : "对不起，查询不到相关数据！",
		"sEmptyTable" : "暂无设备数据存在！",
		"sInfo" : "当前有共 _TOTAL_ 条设备记录",
		"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
		"sSearch" : "搜索",
		"oPaginate" : {
			// "sFirst" : "首页",
			"sPrevious" : "<<",
			"sNext" : ">>",
		// "sLast" : "末页"
		}
	},
	/*
	 * <tr> <td>北京IDC机房链路-互联网网络</td> <td>正常</td> <td>5ms</td> <td>0%</td>
	 * <td>是</td> </tr>
	 */
	ajax : "/nettopo/loadLink",
	columns : [
			{
				"data" : "name"
			},
			{
				"data" : "netstatus",
				render : function(data, type, full) {
					if (data != null) {
						if (data == '1') {
							return '正常';
						} else if (data == '0') {
							return '<lable style="color:#FF0000">不正常</label>';
						} else if (data == undefined) {
							return '<lable style="color:#ffae00">状态未知</label>';
						}
					} else {
						return '-';
					}
				}
			},
			{
				"data" : "delay_time",
				render : function(data, type, full) {
					if (data != null) {
						if (data <= 100) {
							return '<lable style="color:#008800">' + data
									+ 'ms</label>';
						} else {
							return '<lable style="color:#FF0000">' + data
									+ 'ms</label>';
						}
					} else {
						return '-';
					}
				}
			},
			{
				"data" : "losspack",
				render : function(data, type, full) {
					if (data != null) {
						if (data > 10) {
							return '<lable style="color:#FF0000">' + data
									+ '%</label>';

						} else {
							return data + '%';
						}
					} else {
						return '-';
					}
				}
			}, {
				"data" : "netstatus",
				render : function(data, type, full) {
					if (data != null) {
						if (data == '1') {
							return '<lable style="color:#005500">可达</label>';
						} else if (data == '0') {
							return '<lable style="color:#FF0000">不可达</label>';
						} else if (data == undefined) {
							return '<lable style="color:#ffae00">状态未知</label>';
						}
					} else {
						return '-';
					}
				}
			} ]
};

var NT_TOPO_MAP = new Map();
NET_TOPO.loadTopo = function() {
	var canvas = document.getElementById('nt_topo_canvas');
	var stage = new JTopo.Stage(canvas);
	var scene = new JTopo.Scene(stage);
	// scene.background = '../../device/img/plane_inside.png';

	$.ajax({
		url : "/nettopo/loadNettopo",
		success : function(data) {
			var containerWidth = 1388;// 原始比例分母
			// 原型定义新属性
			JTopo.Node.prototype.orginBit = null;
			JTopo.Node.prototype.resize = function() {
				var newX = ($(window).get(0).innerWidth - 50) * this.orginBit;
				this.setLocation(newX, this.y);
			};

			// topo节点
			var deviceList = data.net_topo_node;
			for (var i = 0; i < deviceList.length; i++) {
				var vo = deviceList[i];
				var x = vo.x;
				var y = vo.y;
				var width = vo.width;
				var height = vo.height;
				var node = null;
				if (vo.node_type == 0) {
					var url = vo.url;
					var text = vo.text;
					node = createNode2(x, y, width, height, url, text);
				} else if (vo.node_type == 1) {
					node = createContainer(x, y, width, height);
				} else if (vo.node_type == 2) {
					var text = vo.text;
					node = createTextNode(x, y, text);
				}
				node.showSelected = false;
				node.dragable = false;
				scene.add(node);
				node.orginBit = node.x / containerWidth;
				node.resize();
				NT_TOPO_MAP.put(vo.id, node);
			}

			// topo连线
			var linkList = data.net_topo_link;
			for (var i = 0; i < linkList.length; i++) {
				var vo = linkList[i];
				var aNode = NT_TOPO_MAP.get(vo.aNode);
				var zNode = NT_TOPO_MAP.get(vo.zNode);

				var link = null;
				if (vo.link_type == 0) {
					link = strikeLink(aNode, zNode, vo.text, 'vertical', 7,
							'0,150,0');
				} else {
					link = foldLink(aNode, zNode, vo.text, 'vertical', 7,
							'0,150,0');
				}

				scene.add(link);
			}

			// 比例定点缩放
			$(window).resize(function() {
				NT_TOPO_MAP.each(function(key, value) {
					value.resize();
				});
			});

		}
	});

}
